﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WebSnapshots
{
    public class WebSnapshotsHelper
    {
        Bitmap m_Bitmap;
        string m_Url;
        int m_BrowserWidth, m_BrowserHeight, m_SnapWidth, m_SnapHeight;
        int m_OffsetX, m_OffsetY;
        int m_Timeout;
        private WebSnapshotsHelper(string Url, int BrowserWidth, int BrowserHeight, int SnapWidth, int SnapHeight, int OffsetX, int OffsetY, int Timeout)
        {
            m_Url = Url;
            m_BrowserHeight = BrowserHeight;
            m_BrowserWidth = BrowserWidth;
            m_SnapWidth = SnapWidth;
            m_SnapHeight = SnapHeight;
            m_OffsetX = OffsetX;
            m_OffsetY = OffsetY;
            m_Timeout = Timeout;
        }

        /// <summary>
        /// GetWebSiteThumbnail
        /// </summary>
        /// <param name="Url">目标网络地址</param>
        /// <param name="BrowserWidth">浏览器宽度</param>
        /// <param name="BrowserHeight">浏览器高度</param>
        /// <param name="SnapWidth">截图的宽度</param>
        /// <param name="SnapHeight">截图的高度</param>
        /// <param name="OffsetX">截图偏移量，从左边X处开始截图</param>
        /// <param name="OffsetY">截图偏移量，从上边Y处开始截图</param>
        /// <param name="Timeout">超过N秒后即超时，Bitmap返回null</param>
        /// SnapWidth+OffsetX不能大于SnapWidth，SnapHeight+OffsetY不能大于SnapHeight
        /// <returns></returns>
        public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int SnapWidth, int SnapHeight, int OffsetX = 0, int OffsetY = 0, int Timeout = 60)
        {
            WebSnapshotsHelper thumbnailGenerator = new WebSnapshotsHelper(Url, BrowserWidth, BrowserHeight, SnapWidth, SnapHeight, OffsetX, OffsetY, Timeout);
            return thumbnailGenerator.GenerateWebSiteThumbnailImage();
        }
        private Bitmap GenerateWebSiteThumbnailImage()
        {
            Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteSnapImage));
            m_thread.SetApartmentState(ApartmentState.STA);
            m_thread.Start();
            m_thread.Join();
            return m_Bitmap;
        }
        private void _GenerateWebSiteSnapImage()
        {
            WebBrowser m_WebBrowser = new WebBrowser();
            m_WebBrowser.ScrollBarsEnabled = false;
            //获取或设置一个值，该值指示 WebBrowser 是否显示对话框，如脚本错误消息。
            //如果控件不显示其对话框，则为 true；否则为 false。 默认值为 false。
            m_WebBrowser.ScriptErrorsSuppressed = true;
            m_WebBrowser.Navigate(m_Url);
            m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);

            DateTime dateFrom = DateTime.Now;
            DateTime dateTo;
            while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete || m_WebBrowser.IsBusy)
            {
                dateTo = DateTime.Now;
                if ((dateTo - dateFrom).TotalSeconds < m_Timeout)
                {
                    Application.DoEvents();
                }
                else
                {
                    break;
                }
            }
            m_WebBrowser.Dispose();
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser m_WebBrowser = (WebBrowser)sender;
            m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, this.m_BrowserHeight);
            m_WebBrowser.ScrollBarsEnabled = false;
            m_Bitmap = new Bitmap(m_BrowserWidth, m_BrowserHeight);
            m_WebBrowser.BringToFront();
            m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
            Rectangle rClipRect = new Rectangle(m_OffsetX, m_OffsetY, m_SnapWidth, m_SnapHeight);
            m_Bitmap = m_Bitmap.Clone(rClipRect, m_Bitmap.PixelFormat);
            m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_SnapWidth, m_SnapHeight, null, IntPtr.Zero);

        }

    }

}
